package org.client.otdr.resolver;

import org.client.otdr.OTDRID;
import org.openems.entity.Entity;
import org.openems.entity.Key;

import framework.AccessorHelper;
import framework.UtilityUnit;
import framework.resolver.UnitResolver;

public class TraceResolver implements UnitResolver {

	@Override
	public void resolve(Entity oldEntity, Entity newEntity, AccessorHelper helper) {
		OTDRID id = OTDRID.get();
		Double ior = helper.getDoubleValue(id.IOR);//((NumericEntity)accessor.get(id.IOR)).getValue();
		Double mag = ior / 1.5;
		
//		helper.setDoubleValue(id.MARKERA, helper.getDoubleValue(id.MARKERA) * mag);
//		helper.setDoubleValue(id.MARKERB, helper.getDoubleValue(id.MARKERB) * mag);
//		helper.setDoubleValue(id.MARKERC, helper.getDoubleValue(id.MARKERC) * mag);
		
		Double rangeIOR = UtilityUnit.toDouble(helper.getSelectedItem(id.DISTANCERANGE)) * ior / 1.5;
		helper.setDoubleValue(id.ACTIVETRACERANGEIOR, rangeIOR);
		
	
		for (Key key : helper.getKeyList(id.MARKERLIST)) {
			double markerY = getMarkerY(helper, key);
			helper.setMarkerY(key, markerY);
		}
		
		helper.setMarkerY(id.GOOGLEMAPMARKER, getMarkerY(helper, id.GOOGLEMAPMARKER));
	}

	private double getMarkerY(AccessorHelper helper, Key key) {
		OTDRID id = OTDRID.get();
		Double[] trace = helper.getTraceValues(id.TRACE);
		Double max = helper.getTraceXmax(id.TRACE);
		double x = helper.getMarkerX(key);
		int index = (int)(trace.length * x / max);
		double markerY = trace[index];
		return markerY;
	}

}
